package dhm.com.dhmshop.updata.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.dhmshop.R;
import dhm.com.dhmshop.adapter.home.GoodsConsultAdapter;
import dhm.com.dhmshop.base.BaseActiity;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.GoodsConsult;

public class GoodsconsultActivity extends BaseActiity implements LoginContract.IView {

    @BindView(R.id.recy_data)
    XRecyclerView recyData;
    private PressenterImpl pressenter;
    @BindView(R.id.wu)
    LinearLayout wu;
    @BindView(R.id.you)
    RelativeLayout you;
    private int page;
    private List<GoodsConsult.DataBean> consultData;
    private GoodsConsultAdapter goodsConsultAdapter;
    private String goodsId;

    @Override
    protected int getLayout() {
        return R.layout.activity_goodsconsult;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        pressenter = new PressenterImpl();
        pressenter.attachView(this);
        page = 1;
        goodsId = getIntent().getStringExtra("goodsId");

        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        consultData=new ArrayList<>();
        recyData.setLayoutManager(manager);
        recyData.setLoadingMoreEnabled(true);
        recyData.setPullRefreshEnabled(true);
        recyData.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                consultData=new ArrayList<>();
                Map<String, String> map = new HashMap<>();
                map.put("token", Constant.TOKEN);
                map.put("page", page + "");
                map.put("goodsId", goodsId);
                pressenter.sendMessage(GoodsconsultActivity.this, Constant.GoodsConsult, map, GoodsConsult.class);
            }

            @Override
            public void onLoadMore() {
                Map<String, String> map = new HashMap<>();
                map.put("token", Constant.TOKEN);
                map.put("page", page + "");
                map.put("goodsId", goodsId);
                pressenter.sendMessage(GoodsconsultActivity.this, Constant.GoodsConsult, map, GoodsConsult.class);
            }
        });


        Map<String, String> map = new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("page", page + "");
        map.put("goodsId", goodsId);
        pressenter.sendMessage(this, Constant.GoodsConsult, map, GoodsConsult.class);

    }

    @Override
    protected void initData() {


    }

    @Override
    public void requesta(Object data) {

        if (data instanceof GoodsConsult) {
            GoodsConsult goodsConsult = (GoodsConsult) data;
            if (goodsConsult.getCode() == 1) {
                wu.setVisibility(View.GONE);
                you.setVisibility(View.VISIBLE);
                if (page==1){
                    consultData = goodsConsult.getData();
                    goodsConsultAdapter = new GoodsConsultAdapter(consultData,this);
                    recyData.setAdapter(goodsConsultAdapter);
                }else {
                    consultData.addAll(goodsConsult.getData());
                }
                goodsConsultAdapter.setList(consultData);
                page++;
            } else {
                if (consultData==null||consultData.size()==0){
                    you.setVisibility(View.GONE);
                    wu.setVisibility(View.VISIBLE);
                }
            }
            recyData.refreshComplete();
            recyData.loadMoreComplete();
        }

    }

    @Override
    public void fail(String error) {

    }

    @OnClick({R.id.back, R.id.sure, R.id.consult})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.sure:
                intent=new Intent(this,AddconsultActivity.class);
                intent.putExtra("goodsId",goodsId);
                startActivity(intent);
                break;
            case R.id.consult:
                intent=new Intent(this,AddconsultActivity.class);
                intent.putExtra("goodsId",goodsId);
                startActivity(intent);
                break;
                default:
        }
    }
}
